use sea_orm::DatabaseConnection;
use sha2::{Digest, Sha256};
use snowflake::SnowflakeIdBucket;
use std::{env, sync::Arc};
use tokio::sync::Mutex;

#[derive(Clone)]
pub struct State {
    pub db: DatabaseConnection,
    pub id_bucket: Arc<Mutex<SnowflakeIdBucket>>,
    pub pswd_hasher: Arc<Sha256>,
}

impl State {
    pub fn new(db: DatabaseConnection) -> Self {
        let salt = env::var("SALT").expect("NO `SALT`").into_bytes();

        Self {
            db,
            id_bucket: Arc::new(Mutex::new(SnowflakeIdBucket::new(114, 514))),
            pswd_hasher: Arc::new(Sha256::new_with_prefix(salt)),
        }
    }
}
